package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.TestLooperManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public ListView list;
    public ImageView movie_img;
    public TextView movie_tv;

//    public LinkedList<Movie> movieData = new LinkedList<Movie>(); //创建电影的Linklist
    public List<Movie> movieData = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = findViewById(R.id.my_list);  //先获取主页的Listview布局
        movieData.add(new Movie("肖申克的救赎",R.drawable.movie01,"20世纪40年代末，小有成就的青年银行家安迪（蒂姆·罗宾斯 Tim Robbins 饰）因涉嫌杀害妻子及她的情人而锒铛入狱。在这座名为肖申克的监狱内，希望似乎虚无缥缈，终身监禁的惩罚无疑注定了安迪接下来灰暗绝望的人生。未过多久，安迪尝试接近囚犯中颇有声望的瑞德（摩根·弗里曼 Morgan Freeman 饰），请求对方帮自己搞来小锤子。以此为契机，二人逐渐熟稔，安迪也仿佛在鱼龙混杂、罪恶横生、黑白混淆的牢狱中找到属于自己的求生之道。他利用自身的专业知识，帮助监狱管理层逃税、洗黑钱，同时凭借与瑞德的交往在犯人中间也渐渐受到礼遇。表面看来，他已如瑞德那样对那堵高墙从憎恨转变为处之泰然，但是对自由的渴望仍促使他朝着心中的希望和目标前进。而关于其罪行的真相，似乎更使这一切朝前推进了一步……"));
        movieData.add(new Movie("断背山",R.drawable.movie02,"简介"));
        movieData.add(new Movie("海洋之歌",R.drawable.movie03,"简介"));
        movieData.add(new Movie("阿甘正传",R.drawable.movie04,"简介"));
        movieData.add(new Movie("怪兽大学",R.drawable.movie05,"简介"));
        movieData.add(new Movie("惊天魔盗团",R.drawable.movie06,"简介"));
        movieData.add(new Movie("泰坦尼克号",R.drawable.movie07,"简介"));
        movieData.add(new Movie("控方证人",R.drawable.movie08,"简介"));

        MyBaseAdapter myAdapter = new MyBaseAdapter();  //创建MyBaseAdapter
        list.setAdapter(myAdapter); //将获取到的listview组件setAdapter 参数为创建的MyBaseAdapter

       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Toast.makeText(MainActivity.this,movieData.get(position).getMovie_name(),Toast.LENGTH_LONG).show();
               open(movieData.get(position));
           }
       });
    }

    public void open(Movie movie) {
        Intent intent = new Intent(this,detail_Activity.class);
        intent.putExtra("movie",movie);
        startActivity(intent);
    }

    class MyBaseAdapter extends BaseAdapter { //创建MyBaseAdapter类 继承BaseAdapter类

        //返回item的总数
        @Override
        public int getCount() {
            return movieData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            //将list_item布局转换成一个View对象
//            View view = View.inflate(MainActivity.this, R.layout.list_layout, null);
//            //找到图片和显示文字控件
//            movie_img = view.findViewById(R.id.item_img);
//            movie_tv = view.findViewById(R.id.item_tv);
//            //给图片和文字控件设置背景和标题
//            movie_img.setBackgroundResource(icon[position]);
//            movie_tv.setText(name[position]);

            //优化版
            ViewHolder holder;
            if(convertView == null){
                convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_layout,null);
                holder = new ViewHolder();
                holder.movieImg = convertView.findViewById(R.id.item_img);
                holder.movieTv = convertView.findViewById(R.id.item_tv);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
            holder.movieImg.setBackgroundResource(movieData.get(position).getImg());
            holder.movieTv.setText(movieData.get(position).getMovie_name());
            return convertView;
//            return view;
        }
        }

    class ViewHolder{
        ImageView movieImg;
        TextView movieTv;

    }
}

